$(function () {
    layui.use(["form","layer"],function () {
        var layer = layui.layer;

        //获取input框
        var _input = $("input[name='title']");

        //获取input框焦点
        _input.focus();

        //异步请求保存
        function submit(data) {
            /*$.post("/Posts/post", data,function (res) {  //测试
                if (res.code === 0) {
                    //重新加载页面
                    location.reload();
                } else if (res.code > 0) {
                    layer.msg(res.msg,{icon: 2,time:1000});
                }
            },'json')*/
            $.ajax({
                type: 'post',
                url: '/Posts/post',
                traditional: true,   //传数组需要特定的属性
                data: data,
                success: function (response) {
                    if (response.code === 0) {
                        console.log('yes');
                        //重新加载页面
                        location.reload();
                    } else if (response.code > 0) {
                        layer.msg(response.msg,{icon: 2,time:1000});
                    }
                }
            })
        }

        //富文本编辑器
        var editor = new Simditor({
            textarea: $('#editor'),
            toolbar: [
                // 'title',  //标题
                'bold',  //字体加粗
                'italic',  //字体倾斜
                // 'underline',  //下划线
                // 'strikethrough',  //删除线
                'color',  //字体颜色
                'emoji',  //表情
                // 'ol',  //有序列表
                // 'ul',  //无序列表
                'blockquote',  //引用
                'code',  //代码
                'link',  //连接
                'image',  //上传图片
                // 'hr',  //分割线
                // 'indent',  //向右缩进
                // 'outdent',  //向左缩进
                'alignment'  //水平对其、居左、居中、居右
            ],
            emoji: {
                imagePath: '../static/plugins/simditor-emoji/images/emoji',
                images : [
                    /*'hbf2019_jinli_org.png',
                    '2018new_zhongguozan_org.png',*/
                    '2018new_erha_org.png',
                    /*'2018new_baobao_org.png',
                    '2018new_tanshou_org.png',
                    '2018new_gui_org.png',*/
                    '2018new_doge02_org.png',
                    '2018new_miaomiao_org.png',
                    '2018new_xiongmao_org.png',
                    '2018new_jiyan_org.png',
                    '2018new_xiaoku_thumb.png',
                    '2018new_qinqin_org.png',
                    '2018new_touxiao_org.png',
                    '2018new_kuxiao_org.png',
                    '2018new_xiaoerbuyu_org.png',
                    '2018new_haha_org.png',
                    '2018new_han_org.png',
                    '2018new_aini_org.png',
                    '2018new_chijing_org.png',
                    '2018new_dalian_org.png',
                    '2018new_chigua_org.png',
                    '2018new_chongjing_org.png',
                    '2018new_huaixiao_org.png',
                    '2018new_huaxin_org.png',
                    '2018new_yinxian_org.png',
                    '2018new_yun_org.png',
                    '2018new_zhouma_org.png',
                    '2018new_wenhao_org.png',
                    '2018new_wu_org.png',
                    '2018new_xixi_org.png',
                    '2018new_leimu_org.png',
                    '2018new_ku_org.png',
                    '2018new_kelian_org.png',
                    '2018new_beishang_org.png',
                    '2018new_chanzui_org.png',
                    '2018new_dahaqian_org.png',
                    '2018new_ningwen_org.png',
                    '2018new_nu_org.png',
                    '2018new_qian_org.png',
                    '2018new_shengbing_org.png',
                    '2018new_shuai_org.png',
                    '2018new_shuijiao_thumb.png',
                    '2018new_wabi_thumb.png',
                    '2018new_weiqu_org.png',
                    '2018new_zhuakuang_org.png',
                    '2018new_zuohengheng_org.png',
                    '2018new_youhengheng_org.png',
                    '2018new_heng_org.png',
                    '2018new_guzhang_org.png',
                    '2018new_heixian_org.png',
                    '2018new_tu_org.png',
                    '2018new_kouzhao_org.png',
                    '2018new_tianping_org.png',
                    '2018new_xianhua_org.png',
                ],
            },
            pasteImage: true, //允许粘贴图片
            cleanPaste: true,
            upload: {
                url: '/BBS/uploadImage',  //文件上传的接口地址
                params: null,   //键值对,指定文件上传接口的额外参数,上传的时候随文件一起提交
                fileKey: 'file',   //服务器端获取文件数据的参数名
                connectionCount: 3,
                leaveConfirm: 'Uploading is in progress, are you sure to leave this page?'
            }
        });


        //保存签名事件
        $("#save").click(function () {
            let _data = {};
            //获取input框中的标题
            var title = _input.val();

            //获取文本框的内容
            var content = editor.getValue();

            //非空验证
            if ($.trim(title) === '') {
                layer.msg("签名标题不能为空",{icon:5});
                //获取文本框焦点
                _input.focus();
                //添加一个红色提示框的class
                _input.addClass('layui-form-danger');
                return false;
            } else if ($.trim(content) === '' ) {
                layer.msg("签名内容不能为空",{icon:5});
                //获取文本框焦点
                editor.focus();
                //添加一个红色提示框的class
                $(".publish-box .simditor").addClass('danger');
                return false;
            }
            _data.title = title;
            _data.content = content;
            console.log(_data); //打印信息

            //异步请求保存
            submit(_data);  //渲染时为该条数据添上data-id
        });


        //勾选、编辑、删除签名事件
        $(document).on('click','.action',function (e) {
            e.preventDefault(); //阻止默认事件
            let _data = {};

            _data.id = $(this).parents('.sign-box').attr('data-id');//获取当前按钮的所在框的id

            _data.action = $(this).attr('data-action');//获取当前按钮的action

            //编辑事件需把内容显示到input框和文本框
            if (_data.action === 'edit') {
                _input.val($(this).parents('.sign-box').find('.title').html());  //显示标题
                editor.setValue($(this).parents('.sign-box').find('p').html());   //显示内容
                _input.focus();  //定位input框
            }

            //勾选事件需向用户标注勾选情况
            if (_data.action ==='sure') {
                var _this = $(this);
                layer.confirm('确定要选中该条签名吗？', {icon: 3, title:'提示'}, function(index){
                    console.log(_data);
                    //do something
                    $('.sign-box').removeClass('active'); //移除其他的选中情况
                    _this.parents('.sign-box').addClass('active'); //标注当前选中

                    submit(_data);  //异步请求选中当前签名

                    layer.close(index);
                })
            }

            //删除事件
            if (_data.action ==='del') {
                layer.confirm('确定要删除该条签名吗？', {icon: 3, title:'提示'}, function(index){
                    console.log(_data);
                    //do something
                    submit(_data);  //异步请求删除该条签名

                    layer.close(index);
                })
            }
        });
    })
});